﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Web;
using OrgChart.Entity;
using OrgChart.Logic.Interface;
using App.Service;
using App.Data;

namespace OrgChart.Logic
{
    public class UserLogic: IUser
    {
        public virtual Entity.UserEntity CurrentUser
        {
            get
            {
                if (HttpContext.Current.Session["CurrentUser"] != null)
                {
                    return (UserEntity)HttpContext.Current.Session["CurrentUser"];
                }
                HttpContext.Current.Response.Redirect("/Login.aspx?returnUrl=" + HttpContext.Current.Request.Url.AbsoluteUri);
                return null;
            }
        }

        public virtual void SetCurrentUser(string userAccount)
        {
            UserEntity entity = GetEntityByAccount(userAccount);
            if (entity == null)
            {
                entity = new UserEntity();
                entity.AD_ACCOUNT = userAccount;
            }
            HttpContext.Current.Session["CurrentUser"] = entity;
        }

        public virtual List<UserEntity> GetAll()
        {
            List<UserEntity> lists = DataAccess.Instance("BusinessDB").GetList<UserEntity>("UserLogic_GetAll", null);
            return lists;
        }

        public virtual UserEntity GetEntityByAccount(string account)
        {
            Hashtable table = new Hashtable();
            table.Add("Account", account);
            return DataAccess.Instance("BusinessDB").GetEntity<UserEntity>("UserLogic_GetEntityByAccount", table);
        }


        public virtual string GetFullName(string name)
        {
            throw new NotImplementedException();
        }

        public bool IsLogin
        {
            get
            {
                if (HttpContext.Current.Session["CurrentUser"] == null)
                {
                    return false;
                }
                return true;
            }
        }
    }
}
